##############
# (*) Kaleidoscope
# By: Allan Spale
# August 2008

from Kaleidoscope.KSEventManager import KSEventManagerError
import sys

class KSMappedMixInVirtualTypeError(Exception):
    """
    Exception class for KSColumnMapped

    """
    # REDO ALL CONSTANT PROPERTIES TO BE NORMAL/WRITABLE OUTSIDE OF CLASS...
    # FIX THIS LATER

    B_UNKNOWN_EXCEPTION = True
    B_CLASS_EXCEPTION = False

    B_PRIMARY_COLUMN = True
    B_SECONDARY_COLUMN = False

    def __init__(self, u_message):
        self.__u_message = u_message
    def __str__(self):
        return repr(self.__u_message)

    @staticmethod
    def print_and_raise_error(u_message, b_unknown_exception):
        """
        Convenience method for printing and raising an error for this
        particular Exception subclass.

        """
        #sys.stderr.write(StringIO.StringIO(sys.exc_info()))
        # write initial error
        u_message = u'KSMappedMixInVirtualType.py: ' + u_message
        sys.stderr.write(u_message + u'\n')

        if (b_unknown_exception):
            raise Exception(u_message)
        else:
            raise KSColumnMapped(u_message)


class KSMappedMixInVirtualType(object):
    @property
    def is_open(self):
        return self.__b_open


    def open(self, i_flags, i_permissions, kstx_parent_transaction):
        """
        Open the database file for the column.

        """
        try:
            if not (self.__b_open):
                kstx_transaction = self.get_transaction(kstx_parent_transaction,
                    self.open)
                self.__ksc_key_column.open(
                    i_flags, i_permissions, kstx_transaction)
                kstx_transaction.commit(0, self.open)
                self.__b_open = True
        except Exception:
            kstx_transaction.cancel()
            raise

        return self.__b_open


    def close(self):
        """
        Close the database file for the column.

        """
        try:
            if (self.__b_open):
                self.__ksc_key_column.close()
                self.__b_open = False
        except Exception:
            raise

        return self.__b_open


    def register_before_function_event(self, b_is_primary_column,
                                            st_function_name, fn_callback):
        try:
            if (b_is_primary_column):
                return self.__ksc_key_column.ksem_event_manager.\
                    register_before_function_event(
                        st_function_name, fn_callback)
        except KSEventManagerError:
            KSMappedMixInVirtualTypeError.print_and_raise_error(u'The '
                + u'"before event" function could not be registered.',
                KSMappedMixInVirtualTypeError.B_UNKNOWN_EXCEPTION)


    def delete_before_function_event(self, b_is_primary_column,
                                            st_function_name):
        try:
            if (b_is_primary_column):
                return self.__ksc_key_column.ksem_event_manager.\
                    delete_before_function_event(st_function_name)
        except KSEventManagerError:
            KSMappedMixInVirtualTypeError.print_and_raise_error(u'The '
                + u'"before event" function could not be removed.',
                KSMappedMixInVirtualTypeError.B_UNKNOWN_EXCEPTION)


    def register_after_function_event(self, b_is_primary_column,
                                            st_function_name, fn_callback):
        try:
            if (b_is_primary_column):
                return self.__ksc_key_column.ksem_event_manager.\
                    register_after_function_event(
                        st_function_name, fn_callback)
        except KSEventManagerError:
            KSMappedMixInVirtualTypeError.print_and_raise_error(u'The '
                + u'"after event" function could not be registered.',
                KSMappedMixInVirtualTypeError.B_UNKNOWN_EXCEPTION)


    def delete_after_function_event(self, b_is_primary_column,
                                        st_function_name):
        try:
            if (b_is_primary_column):
                return self.__ksc_key_column.ksem_event_manager.\
                    delete_after_function_event(st_function_name)
        except KSEventManagerError:
            KSMappedMixInVirtualTypeError.print_and_raise_error(u'The '
                + u'"after event" function could not be removed.',
                KSMappedMixInVirtualTypeError.B_UNKNOWN_EXCEPTION)


    def register_secondary_column_events(self):
        pass